package timer;

import java.awt.Color;

import saobracaj.*;

public class Timer extends Thread {
	public int maxCnt;
	private int tmpCnt;
	private Semafor semLN, semPN, semDN, semLS, semPS, semDS;
	// private int treptanjeZ;
	private Timer susedni;
	
	private Semafor susPesackiN, susPesackiS;

	private Object sync = new Object();
	
	private static int IDuk = 0;
	private int id;
	private Raskrsnica rask;
	//TODO timer za pesake
	//0 - sever/jug    1 - istok/zapad
	// private RaskrsnicaGUI raskGUI = RaskrsnicaGUI.dohvatiRaskrsnicu();

	public Timer(Raskrsnica r, int cnt, Semafor sem1, Semafor sem2, Semafor sem3, Semafor sem11, Semafor sem21, Semafor sem31, Semafor pesacki, Semafor pesacki2) {
		maxCnt = cnt;
		this.semLN = sem1;
		this.semPN = sem2;
		this.semDN = sem3;
		this.semLS = sem11;
		this.semPS = sem21;
		this.semDS = sem31;
		this.susPesackiN = pesacki;
		this.susPesackiS = pesacki2;
		id = IDuk++;
		rask = r;
	}

	public int getCnt() {
		return tmpCnt;
	}

	public void setCnt(int cnt) {
		this.tmpCnt = cnt;
	}

	public void resetCnt() {
		tmpCnt = maxCnt;
	}

	public void decCnt() {
		tmpCnt--;
	}

	public Timer getSusedni() {
		return susedni;
	}

	public void setSusedni(Timer susedni) {
		this.susedni = susedni;
	}

	public void signal() {
		synchronized (sync) {
			sync.notify();
		}
	}

	public void run() {

		synchronized (sync) {
			try {
				sync.wait();
			} catch (InterruptedException ex) {
				ex.printStackTrace();
			}
		}

		resetCnt();
		RaskrsnicaGUI raskGUI = RaskrsnicaGUI.dohvatiRaskrsnicu();
		while (true) {
			int nr = 0;
			int sr = 0;
			if ((susPesackiN.prethodniTaster) && (susPesackiN.taster == 0)) nr = 1;
			if ((susPesackiS.prethodniTaster) && (susPesackiS.taster == 0)) sr = 1;
			semPN.postaviCrvenoZuto();
			semPS.postaviCrvenoZuto();
			if(nr == 1) semDN.postaviCrvenoZuto();
			if(sr == 1) semDS.postaviCrvenoZuto();
			System.out.println("postavlja zeleno");
			raskGUI.repaint();
			try {
				Thread.sleep(1000);
				rask.tNorth--;
				rask.tSouth--;
				rask.tEast--;
				rask.tWest--;
				if (rask.tNorth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
				if (rask.tSouth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
				if (rask.tWest!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
				if (rask.tEast!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			
			///////////////////////////////////////////////////////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			
			if ((rask.tNorth == 0) && (rask.severPes.prethodniTaster) && (rask.severPes.taster == 0)){
				rask.tNorth = 2*rask.zelenoL + 2*rask.zelenoP + 40;
			}
			else if (rask.tNorth == 0){
				rask.tNorth = rask.zelenoPesaci + 1;
				RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setForeground(Color.GREEN);
				RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
			}
			
			
			if ((rask.tSouth == 0) && (rask.jugPes.prethodniTaster) && (rask.jugPes.taster == 0)){
				rask.tSouth = 2*rask.zelenoL + 2*rask.zelenoP + 40;
			}
			else if (rask.tSouth ==0){
				rask.tSouth = rask.zelenoPesaci + 1;
				RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setForeground(Color.GREEN);
				RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
			}
			
			
			if ((rask.tWest == 0) && (rask.zapadPes.prethodniTaster) && (rask.zapadPes.taster == 0)){
				rask.tWest = 2*rask.zelenoL + 2*rask.zelenoP + 40;
			}
			else if (rask.tWest ==0){
				rask.tWest = rask.zelenoPesaci + 1;
				RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setForeground(Color.GREEN);
				RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
			}
			
			
			if ((rask.tEast == 0) && (rask.istokPes.prethodniTaster) && (rask.istokPes.taster == 0)){
				rask.tEast = 2*rask.zelenoL + 2*rask.zelenoP + 40;
			}
			else if (rask.tEast ==0){
				rask.tEast = rask.zelenoPesaci + 1;
				RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setForeground(Color.GREEN);
				RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
			}
			
			
			semPN.postaviZeleno();
			semPN.javi();
		//	System.out.println("1");
			semPS.postaviZeleno();
			semPS.javi();
		//	System.out.println("2");
			if (nr == 1){	
					semDN.postaviZeleno();
					semDN.javi();
			}
			if (sr == 1){
				semDS.postaviZeleno();
				semDS.javi();
			}
			
			susPesackiN.postaviZeleno();
			susPesackiS.postaviZeleno();
			raskGUI.repaint();

			/*while (tmpCnt != maxCnt - Raskrsnica.zelenoPesaci) {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				decCnt();
			}
			for (int i = 0; i < 3; i++){
				if (susPesackiN.dohvatiSvetlo() == 3) susPesackiN.postaviIskljuceno(); 
				if (susPesackiS.dohvatiSvetlo() == 3) susPesackiS.postaviIskljuceno();	
				raskGUI.repaint();
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				//decCnt();
				if (susPesackiN.dohvatiSvetlo() == 0) susPesackiN.postaviZeleno(1);
				if (susPesackiS.dohvatiSvetlo() == 0) susPesackiS.postaviZeleno(1);
				raskGUI.repaint();
			}
			susPesackiN.postaviCrveno();
			susPesackiS.postaviCrveno();*/
			int flag = 1;
			int r = 1;
			//MIN ZELENO PRAVO JE ZELENOPESACI + 3
			int n = Raskrsnica.zelenoP;
			int pom = 0;
			if (((id == 0) && !(rask.severPsenz) && !(rask.jugPsenz)) || ((id == 1) && !(rask.istokPsenz) && !(rask.zapadPsenz))){
					n = Raskrsnica.zelenoPesaci + 3; 
					pom = Raskrsnica.zelenoP - Raskrsnica.zelenoPesaci - 3;					
			}


			while (tmpCnt >= maxCnt - n) {
				try {
					Thread.sleep(1000);
					rask.tNorth--;
					rask.tSouth--;
					rask.tEast--;
					rask.tWest--;
					if (rask.tNorth!=0){
						if (rask.tNorth<10) RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setForeground(Color.ORANGE);
						RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
					}
					if (rask.tSouth!=0){
						if (rask.tSouth<10) RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setForeground(Color.ORANGE);
						RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
					}
					if (rask.tWest!=0){
						if (rask.tWest<10) RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setForeground(Color.ORANGE);
						RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
					}
					if (rask.tEast!=0){
						if (rask.tEast<10) RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setForeground(Color.ORANGE);
						RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if ((tmpCnt <= maxCnt - Raskrsnica.zelenoPesaci) && (flag == 1)){
					flag = 0;
					int spn = 0;
					int sps = 0;
					if (susPesackiN.dohvatiSvetlo() == 3) spn = 1;
					if (susPesackiS.dohvatiSvetlo() == 3) sps = 1;
					if (rask.tNorth ==0){
						rask.tNorth = 2*rask.zelenoL + 2*rask.zelenoP - rask.zelenoPesaci + 39;
						RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setForeground(Color.RED);
						RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
					}
					if (rask.tSouth ==0){
						rask.tSouth = 2*rask.zelenoL + 2*rask.zelenoP - rask.zelenoPesaci + 39;
						RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setForeground(Color.RED);
						RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
					}
					if (rask.tWest ==0){
						rask.tWest = 2*rask.zelenoL + 2*rask.zelenoP - rask.zelenoPesaci + 39;
						RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setForeground(Color.RED);
						RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
					}
					if (rask.tEast ==0){
						rask.tEast = 2*rask.zelenoL + 2*rask.zelenoP - rask.zelenoPesaci + 39;
						RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setForeground(Color.RED);
						RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
					}
						if (spn == 1) susPesackiN.setTreptanje(true);
						if (sps == 1) susPesackiS.setTreptanje(true);
						for (int i = 0; i < 3; i++) {// /////////////////////////////
							if (spn == 1) susPesackiN.postaviIskljuceno();
							if (sps == 1) susPesackiS.postaviIskljuceno();
							raskGUI.repaint();
							int c = 0;
							try {
								Thread.sleep(250);
								c++;
								if (c == 4){
									c = 0;
									rask.tNorth--;
									rask.tSouth--;
									rask.tEast--;
									rask.tWest--;
									if (rask.tNorth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
									if (rask.tSouth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
									if (rask.tWest!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
									if (rask.tEast!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
								}
								Thread.sleep(250);
								c++;
								if (c == 4){
									c = 0;
									rask.tNorth--;
									rask.tSouth--;
									rask.tEast--;
									rask.tWest--;
									if (rask.tNorth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
									if (rask.tSouth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
									if (rask.tWest!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
									if (rask.tEast!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
								}
								Thread.sleep(250);
								c++;
								if (c == 4){
									c = 0;
									rask.tNorth--;
									rask.tSouth--;
									rask.tEast--;
									rask.tWest--;
									if (rask.tNorth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
									if (rask.tSouth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
									if (rask.tWest!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
									if (rask.tEast!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
								}
							} catch (InterruptedException e) {
								e.printStackTrace();
							}

							if (spn == 1) susPesackiN.postaviCrveno();
							if (sps == 1) susPesackiS.postaviCrveno();
							raskGUI.repaint();

							try {
								Thread.sleep(250);
								c++;
								if (c == 4){
									c = 0;
									rask.tNorth--;
									rask.tSouth--;
									rask.tEast--;
									rask.tWest--;
									if (rask.tNorth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
									if (rask.tSouth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
									if (rask.tWest!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
									if (rask.tEast!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
								}
								Thread.sleep(250);
								c++;
								if (c == 4){
									c = 0;
									rask.tNorth--;
									rask.tSouth--;
									rask.tEast--;
									rask.tWest--;
									if (rask.tNorth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
									if (rask.tSouth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
									if (rask.tWest!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
									if (rask.tEast!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
								}
								Thread.sleep(250);
								c++;
								if (c == 4){
									c = 0;
									rask.tNorth--;
									rask.tSouth--;
									rask.tEast--;
									rask.tWest--;
									if (rask.tNorth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
									if (rask.tSouth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
									if (rask.tWest!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
									if (rask.tEast!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
								}
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
						if (spn == 1) susPesackiN.setTreptanje(false);
						if (sps == 1) susPesackiS.setTreptanje(false);
						if (spn == 1) susPesackiN.postaviCrveno();
						if (sps == 1) susPesackiS.postaviCrveno();
						raskGUI.repaint();
					
					
				
						for(int i = 0; i < 2; i++){
							try {
								Thread.sleep(1000);
								rask.tNorth--;
								rask.tSouth--;
								rask.tEast--;
								rask.tWest--;
								if (rask.tNorth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
								if (rask.tSouth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
								if (rask.tWest!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
								if (rask.tEast!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
							decCnt();
						}
						if (nr == 0) semDN.postaviCrvenoZuto();
						if (sr == 0) semDS.postaviCrvenoZuto();
						raskGUI.repaint();
						try {
							Thread.sleep(1000);
							rask.tNorth--;
							rask.tSouth--;
							rask.tEast--;
							rask.tWest--;
							if (rask.tNorth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
							if (rask.tSouth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
							if (rask.tWest!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
							if (rask.tEast!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						if (nr == 0){
							semDN.postaviZeleno();
							semDN.javi();
						}
						
						if (sr == 0){
							semDS.postaviZeleno();
							semDS.javi();
						}
						raskGUI.repaint();
					
				} //////////////////////////////////////
				else {	
						decCnt();

						//if (raskrsnica)
				}

			}
			rask.tEast -= pom;
			rask.tWest -= pom;
			rask.tNorth -= pom;
			rask.tSouth -= pom;
			RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
			RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
			RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
			RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);	
			semPN.setTreptanje(true);
			semPS.setTreptanje(true);
			for (int i = 0; i < 3; i++) {
				semPN.postaviIskljuceno();
				semPS.postaviIskljuceno();
				//if (susPesackiN.dohvatiSvetlo() == 3) susPesackiN.postaviIskljuceno(); 
				//if (susPesackiS.dohvatiSvetlo() == 3) susPesackiS.postaviIskljuceno();
				raskGUI.repaint();
				try {
					Thread.sleep(1000);
					rask.tNorth--;
					rask.tSouth--;
					rask.tEast--;
					rask.tWest--;
					if (rask.tNorth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
					if (rask.tSouth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
					if (rask.tWest!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
					if (rask.tEast!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				semPN.postaviZeleno();
				semPS.postaviZeleno();
				//if (susPesackiN.dohvatiSvetlo() == 0) susPesackiN.postaviZeleno(1);
				//if (susPesackiS.dohvatiSvetlo() == 0) susPesackiS.postaviZeleno(1);
				raskGUI.repaint();
				try {
					Thread.sleep(1000);
					rask.tNorth--;
					rask.tSouth--;
					rask.tEast--;
					rask.tWest--;
					if (rask.tNorth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
					if (rask.tSouth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
					if (rask.tWest!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
					if (rask.tEast!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			semPN.setTreptanje(false);
			semPS.setTreptanje(false);
			semPN.postaviZuto();
			semPS.postaviZuto();
			//susPesackiN.postaviCrveno();
			//susPesackiS.postaviCrveno();	
			raskGUI.repaint();
			try {
				Thread.sleep(1000);
				rask.tNorth--;
				rask.tSouth--;
				rask.tEast--;
				rask.tWest--;
				if (rask.tNorth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
				if (rask.tSouth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
				if (rask.tWest!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
				if (rask.tEast!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			semPN.postaviCrveno();
			semPS.postaviCrveno();					
			raskGUI.repaint();
			try {
				Thread.sleep(500);
				rask.tNorth--;
				rask.tSouth--;
				rask.tEast--;
				rask.tWest--;
				if (rask.tNorth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
				if (rask.tSouth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
				if (rask.tWest!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
				if (rask.tEast!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			semLN.postaviCrvenoZuto();
			//semDN.postaviCrvenoZuto();
			semLS.postaviCrvenoZuto();
			//semDS.postaviCrvenoZuto();
			raskGUI.repaint();
			try {
				Thread.sleep(1000);
				rask.tNorth--;
				rask.tSouth--;
				rask.tEast--;
				rask.tWest--;
				if (rask.tNorth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
				if (rask.tSouth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
				if (rask.tWest!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
				if (rask.tEast!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			semLN.postaviZeleno();
			semLN.javi();
			//semDN.postaviZeleno();
			//semDN.javi();
			semLS.postaviZeleno();
			semLS.javi();
			//semDS.postaviZeleno();
			//semDS.javi();
			raskGUI.repaint();
			//int f = 0;
			int pom1 = pom;
			if (((id == 0) && !(rask.severDsenz) && !(rask.jugDsenz) && !(rask.severLsenz) && !(rask.jugLsenz)) || 
				((id == 1) && !(rask.istokDsenz) && !(rask.zapadDsenz) && !(rask.istokLsenz) && !(rask.zapadLsenz))){
				pom1 = pom1 + Raskrsnica.zelenoD/2;	
				rask.tEast -= rask.zelenoD/2;
				rask.tWest -= rask.zelenoD/2;
				rask.tNorth -= rask.zelenoD/2;
				rask.tSouth -= rask.zelenoD/2;
				RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
				RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
				RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
				RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
				
			//	f = 0;
			}
			/*else if (((id == 0) && !(rask.severDsenz) && !(rask.jugDsenz)) || 
					((id == 1) && !(rask.istokDsenz) && !(rask.zapadDsenz))){
					pom1 = pom1 + Raskrsnica.zelenoD/2;	
					f = 2;
				}*/
			/*else if (((id == 0) && !(rask.severLsenz) && !(rask.jugLsenz)) || 
					((id == 1) && !(rask.istokLsenz) && !(rask.zapadLsenz))){
					pom1 = pom1 + Raskrsnica.zelenoD/2;	
					f = 1;
				}*/
			
			
			while (tmpCnt != pom1 + 3) {
				try {
					Thread.sleep(1000);
					rask.tNorth--;
					rask.tSouth--;
					rask.tEast--;
					rask.tWest--;
					if (rask.tNorth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
					if (rask.tSouth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
					if (rask.tWest!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
					if (rask.tEast!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				decCnt();
			}

			for (int i = 0; i < 3; i++) {
				//if ((f == 1) || (f == 0)) 
					semLN.postaviIskljuceno();
				//if (f == 0) 
					semDN.postaviIskljuceno();
				//if ((f == 1) || (f == 0)) 
					semLS.postaviIskljuceno();
				//if (f == 0) 
					semDS.postaviIskljuceno();
				raskGUI.repaint();
				try {
					Thread.sleep(1000);
					rask.tNorth--;
					rask.tSouth--;
					rask.tEast--;
					rask.tWest--;
					if (rask.tNorth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
					if (rask.tSouth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
					if (rask.tWest!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
					if (rask.tEast!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				//if ((f == 1) || (f == 0)) 
				semLN.postaviZeleno();
				//if (f == 0) 
				semDN.postaviZeleno();
				//if ((f == 1) || (f == 0)) 
				semLS.postaviZeleno();
				//if (f == 0) 
				semDS.postaviZeleno();
				raskGUI.repaint();
				try {
					Thread.sleep(1000);
					rask.tNorth--;
					rask.tSouth--;
					rask.tEast--;
					rask.tWest--;
					if (rask.tNorth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
					if (rask.tSouth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
					if (rask.tWest!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
					if (rask.tEast!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

			//if ((f == 1) || (f == 0)) 
			semLN.postaviZuto();
			//if (f == 0) 
			semDN.postaviZuto();
			//if ((f == 1) || (f == 0))
			semLS.postaviZuto();
			//if (f == 0) 
			semDS.postaviZuto();
			raskGUI.repaint();
			try {
				Thread.sleep(1000);
				rask.tNorth--;
				rask.tSouth--;
				rask.tEast--;
				rask.tWest--;
				if (rask.tNorth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
				if (rask.tSouth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
				if (rask.tWest!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
				if (rask.tEast!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			//if ((f == 1) || (f == 0)) 
			semLN.postaviCrveno();
			//if (f == 0) 
			semDN.postaviCrveno();
			//if ((f == 1) || (f == 0)) 
			semLS.postaviCrveno();
			//if (f == 0) 
			semDS.postaviCrveno();
			raskGUI.repaint();
			try {
				Thread.sleep(500);
				rask.tNorth--;
				rask.tSouth--;
				rask.tEast--;
				rask.tWest--;
				if (rask.tNorth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTSlabel().setText("" + rask.tNorth);
				if (rask.tSouth!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTJlabel().setText("" + rask.tSouth);
				if (rask.tWest!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTZlabel().setText("" + rask.tWest);
				if (rask.tEast!=0) RaskrsnicaGUI.dohvatiRaskrsnicu().getTIlabel().setText("" + rask.tEast);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			/*if ((pom1 != pom) && (f != 0)){
				while (tmpCnt != pom) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					decCnt();
				}
	
				for (int i = 0; i < 3; i++) {
					if (f == 0) {
						semLN.postaviIskljuceno();
					}
					if ((f == 1) || (f == 0)) {
						semDN.postaviIskljuceno();
					}
					if (f == 0) {
						semLS.postaviIskljuceno();
					}
					if ((f == 1) || (f == 0)) {
						semDS.postaviIskljuceno();
					}
					raskGUI.repaint();
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					if (f == 0) semLN.postaviZeleno();
					if ((f == 1) || (f == 0)) semDN.postaviZeleno();
					if (f == 0) semLS.postaviZeleno();
					if ((f == 1) || (f == 0)) semDS.postaviZeleno();
					raskGUI.repaint();
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
	
				if  (f == 0) {
					semLN.setTreptanje(false);
					semLN.postaviZuto();
				}
				if ((f == 1) || (f == 0)) {
					semDN.setTreptanje(false);
					semDN.postaviZuto();
				}
				if (f == 0) {
					semLS.setTreptanje(false);
					semLS.postaviZuto();
				}
				if ((f == 1) || (f == 0)) {
					semDS.setTreptanje(false);
					semDS.postaviZuto();
				}

				raskGUI.repaint();
				try {
					Thread.sleep(1500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
	
				if (f == 0)semLN.postaviCrveno();
				if ((f == 1) || (f == 0))semDN.postaviCrveno();
				if (f == 0) semLS.postaviCrveno();
				if ((f == 1) || (f == 0))semDS.postaviCrveno();
				raskGUI.repaint();
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			*/
			
			/*
			 * semP.postaviCrvenoZuto(); raskGUI.repaint();
			 * 
			 * try { Thread.sleep(1000); } catch (InterruptedException e) {
			 * e.printStackTrace(); } semP.postaviZeleno(); raskGUI.repaint();
			 */
			resetCnt();
			susedni.signal();
			synchronized (sync) {
				try {
					sync.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

		}
	}
	
	
	public int dohvatiBrojac() {
		return tmpCnt;
	}
}
